using UnityEngine;
using System.Collections;
using System;
/* TOM WAZ ERE */

public class CameraScript: MonoBehaviour {
	float dragSpeed = 100;
	float maxSize = 4000f;
	float maxZoomFOV = 100;
	float minSize = 100f;
	float minZoomFOV = 5;
	private float orbitRotY = 180.0f;
	float sizeZoomSpeed = 50f;
	private float translateX = 0.0f;
	private float translateZ = 0.0f;
	float zoomSpeed = 5f;

	public void lookAtOrbit(Vector3 position, float rotY) {
		rotY += 180;

				float difference = rotY - orbitRotY;
				while (difference < -180) difference += 360;
				while (difference > 180) difference -= 360;

		orbitRotY = 0;//+= difference * 2.3f * Time.deltaTime; // Camera damping

		transform.position = new Vector3(position.x + 200.0f * (float)Math.Sin(orbitRotY * (Math.PI/180) + translateX *0.001f),
																		 200.0f + translateZ * 0.3f,
																		 position.z + 600.0f * (float)Math.Cos(orbitRotY * (Math.PI/180) + translateX *0.001f));
		transform.LookAt(position);
	}

	public void resetView() {
		translateX = 0.0f;
		translateZ = 0.0f;
		Camera.main.fieldOfView = 40f;
		Camera.main.orthographicSize = 1576.195f;
	}

	public void setOverview(float centreX, float centreZ) {
		transform.position = new Vector3(7400+translateX,4200,6000+translateZ);
		transform.LookAt(new Vector3(centreX+translateX, 0.0f, centreZ+translateZ));
	}

	void Start() {
		resetView();
		Camera.main.orthographic = false;
	}

	void Update() {
		// Zoom in, scroll middle mouse button
		if(Input.GetAxis("Mouse ScrollWheel") > 0)
		{
			Camera.main.fieldOfView -= zoomSpeed;
			if (Camera.main.fieldOfView < minZoomFOV)
						Camera.main.fieldOfView = minZoomFOV;

			Camera.main.orthographicSize -= sizeZoomSpeed;
			if (Camera.main.orthographicSize < minSize)
						Camera.main.orthographicSize = minSize;
		}


		// Zoom out, scroll middle mouse button
		if(Input.GetAxis("Mouse ScrollWheel") < 0)
		{
			Camera.main.fieldOfView += zoomSpeed;
			if (Camera.main.fieldOfView > maxZoomFOV)
				Camera.main.fieldOfView = maxZoomFOV;

			Camera.main.orthographicSize += sizeZoomSpeed;
			if (Camera.main.orthographicSize > maxSize)
				Camera.main.orthographicSize = maxSize;
		}

		// Translate camera, hold middle mouse button
		if(Input.GetMouseButton(1))
		{
			translateX += -Input.GetAxis("Mouse X")*dragSpeed;
			translateZ += -Input.GetAxis("Mouse Y")*dragSpeed;
		}
	}
}

